#!/usr/bin/python
# yongming.li

import string
import os
from pychartdir import *

class BatteryMonitor:

        ########################################################
        
	def __init__(self):
            self.name = 'MiscMonitor'
            
            self.filename = './out/battery.txt'

            mypath = 'adb shell cat /sys/class/power_supply/battery/'
            self.mybattery={
                      'status': [mypath + 'status',''],
                      'health':[mypath + 'health',''],
                      'capacity':[mypath + 'capacity',''],
                      'technology':[mypath + 'technology',''],
                      'voltage':[mypath + 'batt_vol',''],
                      'temperature':[mypath + 'batt_temp',''],
                     }
            mypath = 'adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/'

            self.cpu={
                      'cpuinfo_max_freq': mypath + 'cpuinfo_max_freq',
                      'cpuinfo_min_freq':mypath + 'cpuinfo_min_freq',
                      'scaling_cur_freq':mypath + 'scaling_cur_freq',
                      'scaling_max_freq':mypath + 'scaling_max_freq',
                      'scaling_min_freq':mypath + 'scaling_min_freq',
                      'scaling_governor':mypath + 'scaling_governor',
                     }
            
      
        
        def  parsebattery(self,s,name): 
            s=s.strip()
            #sitem=string.split(s)
            os.system('echo %s : %s >> '%(name,s) + self.filename)
            if name == 'capacity':
               self.capacity = s;

        def  createChart(self,img,myvalue):

             # The value to display on the meter
             value = myvalue

             # Create an AugularMeter object of size 200 x 100 pixels with rounded corners
             m = AngularMeter(200, 100)
             m.setRoundedFrame()

             # Set meter background according to a parameter
            
             m.setBackground(goldColor(), 0x000000, -2)
            

             # Set the meter center at (100, 235), with radius 210 pixels, and span from -24
             # to +24 degress
             m.setMeter(100, 235, 210, -24, 24)

             # Meter scale is 0 - 100, with a tick every 1 unit
             m.setScale(0, 100, 20)

             # Set 0 - 6 as green (99ff99) zone, 6 - 8 as yellow (ffff00) zone, and 8 - 10 as
             # red (ff3333) zone
             m.addZone(0, 60, 0x99ff99, 0x808080)
             m.addZone(60, 80, 0xffff00, 0x808080)
             m.addZone(80, 100, 0xff3333, 0x808080)

             # Add a title at the bottom of the meter using 10 pts Arial Bold font
             m.addTitle2(Top, "capacity", "arialbd.ttf", 10)

             # Add a semi-transparent black (80000000) pointer at the specified value
             m.addPointer(value, 0x80000000)

             # Output the chart
             m.makeChart('./out/'+'%s.png' % img)

        def  start(self):
             os.system('echo  > '+ self.filename)
             for item in self.mybattery:
                 lines = os.popen(self.mybattery[item][0]).readlines()
                 for  line  in lines: 
                     self.parsebattery(line,item)

             for item in self.cpu:
                 lines = os.popen(self.cpu[item]).readlines()
                 for  line  in lines: 
                     break
             os.system('adb shell cat /proc/uptime > ./out/uptime.txt ')
            #self.chart('battery')  
             self.createChart('batterycap',self.capacity)

       
